{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Escaping particles\n",
    "\n",
    "Sometimes we are not interested in particles that get too far from the central body.  Here we will define a radius beyond which we remove particles from the simulation.  Let's set up an artificial situation with 3 planets, and the inner one moves radially outward with $v > v_{escape}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "---------------------------------\n",
      "REBOUND version:     \t2.19.1\n",
      "REBOUND built on:    \tJul  8 2016 19:40:49\n",
      "Number of particles: \t4\n",
      "Selected integrator: \tias15\n",
      "Simulation time:     \t0.000000\n",
      "Current timestep:    \t0.001000\n",
      "---------------------------------\n",
      "<rebound.Particle object, m=1.0 x=0.0 y=0.0 z=0.0 vx=0.0 vy=0.0 vz=0.0>\n",
      "<rebound.Particle object, m=0.0 x=0.4 y=0.0 z=0.0 vx=5.0 vy=0.0 vz=0.0>\n",
      "<rebound.Particle object, m=0.0 x=0.7 y=0.0 z=0.0 vx=0.0 vy=1.1952286093343936 vz=0.0>\n",
      "<rebound.Particle object, m=0.0 x=1.0 y=0.0 z=0.0 vx=0.0 vy=1.0 vz=0.0>\n",
      "---------------------------------\n"
     ]
    }
   ],
   "source": [
    "import rebound\n",
    "import numpy as np\n",
    "def setupSimulation():\n",
    "    sim = rebound.Simulation()\n",
    "    sim.add(m=1., hash=\"Sun\")\n",
    "    sim.add(x=0.4,vx=5., hash=\"Mercury\")\n",
    "    sim.add(a=0.7, hash=\"Venus\")\n",
    "    sim.add(a=1., hash=\"Earth\")\n",
    "    sim.move_to_com()\n",
    "    return sim\n",
    "\n",
    "sim = setupSimulation()\n",
    "sim.status()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now let's run a simulation for 20 years (in default units where $G=1$, and thus AU, yr/2$\\pi$, and $M_\\odot$, see [Units.ipynb](../Units) for how to change units), and set up a 50 AU sphere beyond which we remove particles from the simulation.  We can do this by setting the `exit_max_distance` flag of the simulation object.  If a particle's distance (from the origin of whatever inertial reference frame chosen) exceeds `sim.exit_max_distance`, an exception is thrown.\n",
    "\n",
    "If we simply call `sim.integrate()`, the program will crash due to the unhandled exception when the particle escapes, so we'll create a `try`-`except` block to catch the exception.  We'll also store the x,y positions of Venus, which we expect to survive."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A particle escaped (r>exit_max_distance).\n"
     ]
    }
   ],
   "source": [
    "sim = setupSimulation() # Resets everything\n",
    "sim.exit_max_distance = 50.\n",
    "Noutputs = 1000\n",
    "times = np.linspace(0,20.*2.*np.pi,Noutputs)\n",
    "xvenus, yvenus = np.zeros(Noutputs), np.zeros(Noutputs)\n",
    "for i,time in enumerate(times):\n",
    "    try:\n",
    "        sim.integrate(time)  \n",
    "    except rebound.Escape as error:\n",
    "        print(error)\n",
    "        for j in range(sim.N):\n",
    "            p = sim.particles[j]\n",
    "            d2 = p.x*p.x + p.y*p.y + p.z*p.z\n",
    "            if d2>sim.exit_max_distance**2:\n",
    "                index=j # cache index rather than remove here since our loop would go beyond end of particles array\n",
    "        sim.remove(index=index)\n",
    "    xvenus[i] = sim.particles[2].x\n",
    "    yvenus[i] = sim.particles[2].y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Went down to 3 particles\n"
     ]
    }
   ],
   "source": [
    "print(\"Went down to {0} particles\".format(sim.N))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So this worked as expected.  Now let's plot what we got:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAA3YA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XpF3NbGi8\nvUKeadQ/ODa4QXkUGtr03Mwulh8Oc2JWOoR8k9Gm9yhuiWEvkyU97px7Pu7+IGcdJ+lsMztdUjtJ\nHczsMefcsJj7hdy0XNIy59w7ifPJkljIC3X5iaTFzrl1kmRmUyQdK4nkBeqyysy6OudWmVk3SV81\n5odybiimmZ0mPxTmbOfc9rj7g5zEpvfIxKOS/uGcuzvujiB3Oed+5Zzbxzl3gPx/U2YS1KEuiSFS\ny8zsoMSlk8SiO6jbUklHm1lbMzP5zwuL7SBV7ZEiUyVdnDgeLqlR/zAdS8auAfdKai3pZf/Z15vO\nuf+It0vIJc65KjMLNr0vkTSeTe+RjpkdJ+mnkuaZ2fvyQxl+5Zx7Md6eASgAP5f0pJm1krRY0oiY\n+4Mc5ZyarGFFAAAAdUlEQVR728wmS3pfUkWifCjeXiFXmNlTkkolfcfMlkq6WdJtkp4xs0skfS5p\ncKPqYoNyAAAAAMhvOTcUEwAAAACQGQI7AAAAAMhzBHYAAAAAkOcI7AAAAAAgzxHYAQAAAECeI7AD\nAAAAgDxHYAcAAAAAee7/A6tlMUZaRrzlAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x107ca6128>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "fig,ax = plt.subplots(figsize=(15,5))\n",
    "ax.plot(xvenus, yvenus)\n",
    "ax.set_aspect('equal')\n",
    "ax.set_xlim([-2,10]);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This doesn't look right.  The problem here is that when we removed `particles[1]` from the simulation, all the particles got shifted down in the `particles` array.  So following the removal, `xvenus` all of a sudden started getting populated by the values for Earth (the new `sim.particles[2]`).  A more robust way to access particles is using hashes (see [UniquelyIdentifyingParticlesWithHashes.ipynb](../UniquelyIdentifyingParticlesWithHashes))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A particle escaped (r>exit_max_distance).\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAA3YAAACQCAYAAABEWPmPAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAFydJREFUeJzt3XuQpXV95/H3Z2aYEuUixMxwUxJRHDWrFFULeCuPQsx4\nHbJVWhPdCKZSWklQElMuA+vGtmqDkkQTQnQ3XoNbKotoLbMplYGFU5a1iLiCIvQwIGYcmDDEC6yo\n2ZrLd/84Z+gzPd3T3XNOn+ec7ver6lT/nuf8zvP79jNPTZ1v/26pKiRJkiRJ42tF0wFIkiRJkvpj\nYidJkiRJY87ETpIkSZLGnImdJEmSJI05EztJkiRJGnMmdpIkSZI05gaS2CVZn2Rrkm1JLpnh/WOS\nbE5yZ5K7klw4iHYlSZIkSZB+97FLsgLYBpwL7ARuBzZW1daeOpcCx1TVpUmeBtwLrK2qPX01LkmS\nJEkaSI/dWcB9VbW9qnYD1wAbptUp4Ohu+WjgxyZ1kiRJkjQYg0jsTgZ29Bw/2D3X6++A5yXZCXwH\nuHgA7UqSJEmSgFVDaue3gDuq6pVJTgNuTPKCqnp8esUk/Y0NlSRJkqQxV1VZSP1BJHYPAc/oOT6l\ne67X24APAFTV95P8AFgHfGumC/Y770/Lw8TEBBMTE02HoTHh86L58lnRQvi8aL58VrQQyYJyOmAw\nQzFvB56V5NQkq4GNwOZpdbYD5wEkWQucDjwwgLYlSZIkadnru8euqvYmuQjYQidR/GRVTSZ5R+ft\n+hjwn4F/SPLd7sf+Q1X9pN+2JUmSJEkDmmNXVV8FnjPt3N/3lP+Zzjw7aWBarVbTIWiM+LxovnxW\ntBA+L5ovnxUttr73sRu0JDVqMUmSJEnSsCRZ8OIpg5hjJy2qKnjf+yA59Ou3fxv27m06WkmSJGn4\n7LHTyNizB444YnGu/ctfwpOetDjXliRJ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1bpUH4d+FGS\nT3eH7n4syZFNB6XRVFU7gQ/RWSn+IeDRqrqp2ag04tZU1S7o/JEaWDOfDzWS2CW5sTvGeP/rru7P\n1/fU+Y/A7tk2Mpek+UpyFHAdcHG35046QJLXAru6PbzpvqTZrALOBD5SVWcCv6AzdEo6SJKn0umB\nORU4CTgqyZubjUpjZl5/cJxzg/LFcKhNzwGSXEhnOMwrhxKQxs1DwDN6jk/pnpMO0h32ch3w36pq\npr02JYCXAG9I8hrgSODoJJ+pqrc2HJdG04PAjqr6Vvf4OsCFvDSb84AHquonAEm+BLwYsPNCs9mV\nZG1V7UpyAvDIfD40ckMxk6ynMxTmDVX1/5qORyPpduBZSU7triq1EXAFO83mU8A9VXVl04FodFXV\nZVX1jKp6Jp3/U242qdNsukOkdiQ5vXvqXFx0R7P7IXBOkiclCZ3nxcV21Gv6SJHNwIXd8gXAvP4w\n3UiP3RyuAlYDN3aefb5RVX/YbEgaJVW1N8lFdFZQXQF8sqr8D1IHSfIS4C3AXUnuoDOU4bKq+mqz\nkUlaAt4FfDbJEcADwNsajkcjqqq+meQ64A5gd/fnx5qNSqMiyeeAFvArSX4IvA/4IPCFJL8HbAfe\nNK9ruUG5JEmSJI23kRuKKUmSJElaGBM7SZIkSRpzJnaSJEmSNOZM7CRJkiRpzJnYSZIkSdKYM7GT\nJEmSpDFnYidJkiRJY+7/AxjuAhvnKYIkAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x1046c6c18>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "sim = setupSimulation() # Resets everything\n",
    "sim.exit_max_distance = 50.\n",
    "Noutputs = 1000\n",
    "times = np.linspace(0,20.*2.*np.pi,Noutputs)\n",
    "xvenus, yvenus = np.zeros(Noutputs), np.zeros(Noutputs)\n",
    "for i,time in enumerate(times):\n",
    "    try:\n",
    "        sim.integrate(time)  \n",
    "    except rebound.Escape as error:\n",
    "        print(error)\n",
    "        for j in range(sim.N):\n",
    "            p = sim.particles[j]\n",
    "            d2 = p.x*p.x + p.y*p.y + p.z*p.z\n",
    "            if d2>sim.exit_max_distance**2:\n",
    "                index=j # cache index rather than remove here since our loop would go beyond end of particles array\n",
    "        sim.remove(index=index)\n",
    "    xvenus[i] = sim.particles[\"Venus\"].x\n",
    "    yvenus[i] = sim.particles[\"Venus\"].y\n",
    "\n",
    "fig,ax = plt.subplots(figsize=(15,5))\n",
    "ax.plot(xvenus, yvenus)\n",
    "ax.set_aspect('equal')\n",
    "ax.set_xlim([-2,10]);"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Much better!  We solved the problem by assigning particles hashes and using those to access the particles for output."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
